const path = require('path')
const fs = require('fs')
const { isMatch, infoLog } = require('../util.js')
const makeDir = require('make-dir')

/**
 * vuepress v2 侧边栏处理
 * @class SidebarV2
 */
class SidebarV2 {
  /**
   * Creates an instance of SidebarV2.
   * @param {*} savePath 输出的config.js保存路径
   * @param {*} rotuerName 路由名称
   * @param {*} sidebarTree 侧边栏分类
   * @param {*} defaultSiebarName 默认侧边栏名称
   * @param {*} isOuputInfo 是否输出详情日志
   * @memberof SidebarV2
   */
  constructor(savePath, rotuerName, sidebarTree, defaultSiebarName, isOuputInfo) {
    this.savePath = savePath
    this.rotuerName = rotuerName
    this.sidebarTree = sidebarTree
    this.isOuputInfo = isOuputInfo
    this.defaultSiebarName = defaultSiebarName
  }
  /**
   * 创建侧边栏列表
   * @memberof SidebarV2
   */
  createSidebarList() {
    let sidebarList = {}
    let rotuerCode = `/${this.rotuerName}/`
    sidebarList[rotuerCode] = []

    let defaultSidebar = { text: this.defaultSiebarName, children: [] }
    sidebarList[rotuerCode].push(defaultSidebar)

    for (const treeItem of this.sidebarTree) {
      let sidebar = { text: treeItem['title'], children: [] }
      sidebarList[rotuerCode].push(sidebar)
    }
    return sidebarList
  }

  /**
   * 获取自定义侧边栏的标题，用于添加到对应的侧边栏分类
   * @param {*} mdItem
   * @return {*}
   * @memberof SidebarV2
   */
  getTreeTitle(mdItem) {
    let fromPath = mdItem['fromPath']
    for (const treeItem of this.sidebarTree) {
      let includeArr = treeItem['include']
      let excludeArr = treeItem['exclude']
      if (isMatch(includeArr, fromPath) && !isMatch(excludeArr, fromPath)) {
        return treeItem['title']
      }
    }
    return this.defaultSiebarName
  }

  /**
   * 添加到侧边栏
   * @param {*} sidebarList
   * @param {*} mdItem
   * @return {*}
   * @memberof SidebarV2
   */
  addSidebarChildren(sidebarList, mdItem) {
    let sidebarText = this.getTreeTitle(mdItem)
    for (const rotuerName in sidebarList) {
      let sidebarItem = sidebarList[rotuerName]
      for (const sidebar of sidebarItem) {
        // console.log('addSidebarChildren：', rotuerName, sidebar['text'], sidebarText, sidebar['text'] === sidebarText, sidebarList)
        if (sidebar['text'] === sidebarText) {
          let childrenPath = mdItem['mdRelative']
          childrenPath = childrenPath.split(path.sep).join('/')
          sidebar['children'].push(childrenPath)
          return sidebarList
        }
      }
    }
    return sidebarList
  }

  /**
   * 生成vuepress sidebar的侧边栏配置
   * @param {*} mdItemArr
   * @memberof SidebarV2
   */
  saveSidebarConfig(mdItemArr) {
    // 生成sidebar
    let sidebarList = this.createSidebarList()

    // 添加到侧边栏
    for (const mdItem of mdItemArr) {
      sidebarList = this.addSidebarChildren(sidebarList, mdItem)
    }

    let str = `
exports.getApiSidebarList = function () {
  let list = JSON.parse('${JSON.stringify(sidebarList)}')
  return list
}`
    let dirPath = makeDir.sync(path.resolve(this.savePath, this.rotuerName))
    let sidebarFilePath = path.resolve(dirPath, 'config.js')
    this.isOuputInfo && infoLog('输出Vuepress侧边栏配置', sidebarFilePath)
    fs.writeFileSync(sidebarFilePath, str)
  }
}

module.exports = SidebarV2
